package com.hzcx.app.simplechat.ui.setting.presenter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;

import com.hzcx.app.simplechat.api.BaseDialogObserver;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.base.BasePresenter;
import com.hzcx.app.simplechat.bean.UpLoadImgBean;
import com.hzcx.app.simplechat.model.PublicModel;
import com.hzcx.app.simplechat.model.SettingModel;
import com.hzcx.app.simplechat.ui.setting.bean.ReportBean;
import com.hzcx.app.simplechat.ui.setting.contract.ReportContract;
import com.hzcx.app.simplechat.ui.setting.contract.UpdatePhoneContract;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.file.FileUtils;
import com.hzcx.app.simplechat.util.token.TokenUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ReportPresenter extends BasePresenter<ReportContract.View> implements ReportContract.Presenter {

    private int upLoadIndex = 0;
    private List<String> urlList = new ArrayList<>();

    /**
     * 获取举报内容列表
     *
     * @param context
     */
    @Override
    public void getReportList(Context context) {
        SettingModel.getReportList(context, new BaseDialogObserver<List<ReportBean>>(context) {
            @Override
            protected void onSuccess(List<ReportBean> data) {
                mView.reportResult(data);
            }
        });
    }

    /**
     * 提交举报信息
     *
     * @param context
     * @param member_id
     * @param complaint_id
     * @param complaint_content
     * @param complaint_images
     */
    @Override
    public void putReport(Context context, int member_id, int complaint_id, String complaint_content, String complaint_images) {
        SettingModel.putReport(context, member_id, complaint_id, complaint_content, complaint_images, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                mView.hideLoading();
                mView.putSuccess();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                mView.hideLoading();
            }
        });
    }

    /**
     * 上传图片
     *
     * @param context
     * @param pathList
     */
    @Override
    public void upLoadImg(Activity context, List<String> pathList) {

        if (pathList == null || pathList.size() <= 0) {
            mView.upLoadSuccess("");
            return;
        }

        if (EmptyUtils.isEmpty(pathList.get(upLoadIndex))) {
            uploadSuccess(context, pathList);
            return;
        }
        String url;
        File file = new File(pathList.get(upLoadIndex));
        if (file.exists()) {
            url = file.getPath();
        } else {
            url = FileUtils.getImageAbsolutePath(context, Uri.parse(pathList.get(upLoadIndex)));
        }

        PublicModel.upLoadImg(context, TokenUtils.getTokenHeader(), url, new BaseObserver<UpLoadImgBean>() {
            @Override
            protected void onSuccess(UpLoadImgBean data) {
                urlList.add(data.getUrl());
                uploadSuccess(context, pathList);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                mView.hideLoading();
                upLoadIndex = 0;
                urlList.clear();
            }
        });
    }

    /**
     * 图片或视频上传完成
     *
     * @param context
     * @param imgUrls
     */
    private void uploadSuccess(Activity context, List<String> imgUrls) {
        upLoadIndex++;
        if (upLoadIndex <= (imgUrls.size() - 1)) {
            upLoadImg(context, imgUrls);
        } else {
            String images = "";
            for (int i = 0; i < urlList.size(); i++) {
                if (i < (urlList.size() - 1)) {
                    images = images + urlList.get(i) + ",";
                } else {
                    images = images + urlList.get(i);
                }
            }
            upLoadIndex = 0;
            urlList.clear();
            mView.upLoadSuccess(images);
        }
    }

}
